<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="学期" prop="semId">
        <el-select v-model="queryParams.semId" filterable clearable placeholder="请选择学期">
          <el-option
            v-for="item in semsterList"
            :key="item.id"
            :label="item.title"
            :value="item.id"
            size="small">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
   

    <el-table v-loading="loading" :data="studentList">
      <el-table-column label="学期" align="center" prop="semName" />
      <el-table-column label="考核日期" align="center" prop="dates" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.dates, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="规则" align="center" prop="ruleName" />
      <el-table-column label="计分规则" align="center" prop="mode" :formatter="modeFormat" />
      <el-table-column label="分数" align="center" prop="score" />
      <el-table-column label="节次" align="center" prop="sectionName" />
      <el-table-column label="详细说明" align="center" prop="remark" />
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改量化考核对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="学生" prop="stuId">
          <el-input v-model="form.stuId" placeholder="请输入学生" />
        </el-form-item>
        <el-form-item label="规则" prop="ruleId">
          <el-input v-model="form.ruleId" placeholder="请输入规则" />
        </el-form-item>
        <el-form-item label="计分规则">
          <el-radio-group v-model="form.mode">
            <el-radio
              v-for="dict in modeOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="分数" prop="score">
          <el-input v-model="form.score" placeholder="请输入分数" />
        </el-form-item>
        <el-form-item label="考核日期" prop="dates">
          <el-date-picker clearable size="small"
            v-model="form.dates"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择考核日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="节次" prop="sectionId">
          <el-input v-model="form.sectionId" placeholder="请输入节次" />
        </el-form-item>
        <el-form-item label="详细说明" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入详细说明" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { myStudent, getStudent, delStudent, addStudent, updateStudent, exportStudent } from "@/api/quantify/student";
import { selectSemster} from "@/api/base/semster";

export default {
  name: "Student",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 量化考核表格数据
      studentList: [],
      // 学期数据
      semsterList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 计分规则字典
      modeOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        stuId: null,
        ruleId: null,
        mode: null,
        score: null,
        dates: null,
        sectionId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        stuId: [
          { required: true, message: "学生不能为空", trigger: "blur" }
        ],
        ruleId: [
          { required: true, message: "规则不能为空", trigger: "blur" }
        ],
        score: [
          { required: true, message: "分数不能为空", trigger: "blur" }
        ],
        dates: [
          { required: true, message: "考核日期不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("score_mode").then(response => {
      this.modeOptions = response.data;
    });
    selectSemster({'status':1}).then(response => {
      this.semsterList = response.rows;
    });
  },
  methods: {
    /** 查询量化考核列表 */
    getList() {
      this.loading = true;
      myStudent(this.queryParams).then(response => {
        this.studentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 计分规则字典翻译
    modeFormat(row, column) {
      return this.selectDictLabel(this.modeOptions, row.mode);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        stuId: null,
        ruleId: null,
        mode: "0",
        score: null,
        dates: null,
        sectionId: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加量化考核";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getStudent(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改量化考核";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateStudent(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStudent(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除量化考核编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delStudent(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有量化考核数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportStudent(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
